//
//  JXHPicturePickerCollectionView.swift
//  JXHSinaWeiBo
//
//  Created by juxiaohui on 16/10/28.
//  Copyright © 2016年 juxiaohui. All rights reserved.
//

import UIKit

private let picturePickerCell = "picturePickerCell"
private let edgeMargin : CGFloat = 10


class JXHPicturePickerCollectionView: UICollectionView {
    
    var images : [UIImage] = [UIImage]() {
        didSet{
            reloadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        
        dataSource = self
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemHW = (UIScreen.main.bounds.width - 4 * edgeMargin) / 3
        layout.itemSize = CGSize.init(width: itemHW, height: itemHW)
        layout.minimumLineSpacing = edgeMargin
        layout.minimumInteritemSpacing = edgeMargin
        
        contentInset = UIEdgeInsets.init(top: edgeMargin, left: edgeMargin, bottom: 0, right: edgeMargin)
    
        register(UINib.init(nibName: "JXHPicturePickerCell", bundle: nil), forCellWithReuseIdentifier: picturePickerCell)
    }
}

extension JXHPicturePickerCollectionView : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picturePickerCell, for: indexPath) as! JXHPicturePickerCell
        
        cell.image = indexPath.item <= images.count - 1 ? images[indexPath.item] : nil
    
        return cell
        
    }
}
